###############################################################################   
#### Replication Materials                                                 #### 
#### Kim, Nakka, Gopal, Desmrais, Mancinelli, Harden, Ko, Boehmke. 2021.   ####
#### Attention to the COVID-19 pandemic on Twitter:                        ####
#### Partisan differences among U.S. state legislators                     ####
#### Legislative Studies Quarterly                                         ####
###############################################################################  


###############################################################################
################################### Set Up ####################################
###############################################################################

# packages -------------------------

lapply(c('readr', 'stargazer', 'texreg', 'xtable', 'lme4', 'glmmTMB','plm',
         'lmtest', 'interplot','dummies','effects', 'ggthemes','caret', 
         'ggeffects', 'scales','ggrepel','sandwich','reshape2'), 
       require, 
       character.only = TRUE)

# read data set for regression -------------------------

path_data <- '/Users/taegyoon/Google Drive/spap_state/spap_state_attention/data/'

df <- read_csv(paste0(path_data, "spap_state_attention_regression.csv"))
df <- subset(df, select = -c(X1) )
df_uni <- df[which(df$unified_gov == TRUE), ]
df_spt <- df[which(df$unified_gov == FALSE), ]


# set the panel structure -------------------------

plm_df_uni <- pdata.frame(x = df_uni, 
                          index = c('user.screen_name', 'week'))
plm_df_spt <- pdata.frame(x = df_spt, 
                          index = c('user.screen_name', 'week'))


###############################################################################
########## Table S10: Regression by Partisanship-Match Observations ###########
###############################################################################

# fit models -------------------------

uni_state <- plm(covid_relevant_1_log ~ 
                   state_case_pop + 
                   state_death_pop +
                   state_covid_policy + 
                   party_new +
                   week_new +
                   week_new_2 +
                   week_new_3,
                 data = plm_df_uni,
                 index = c('user.screen_name', 'week'),
                 model = 'random', 
                 effect = 'twoways')

uni_national <- plm(covid_relevant_1_log ~ 
                      national_case_pop + 
                      national_death_pop +
                      state_covid_policy + 
                      party_new +
                      week_new +
                      week_new_2 +
                      week_new_3,                                             
                    data = plm_df_uni,
                    index = c('user.screen_name', 'week'),
                    model = 'random',
                    effect = 'twoways')

uni_all <- plm(covid_relevant_1_log ~ 
                 state_case_pop + 
                 state_death_pop +
                 national_case_pop + 
                 national_death_pop +
                 state_covid_policy + 
                 party_new +
                 week_new +
                 week_new_2 +
                 week_new_3,            
               data = plm_df_uni,
               index = c('user.screen_name', 'week'),
               model = 'random', 
               effect = 'twoways')

uni_state_inter <- plm(covid_relevant_1_log ~ 
                         party_new*state_case_pop + 
                         party_new*state_death_pop +
                         party_new*state_covid_policy + 
                         week_new +
                         week_new_2 +
                         week_new_3,            
                       data = plm_df_uni,
                       index = c('user.screen_name', 'week_new'),
                       model = 'random', 
                       effect = 'twoways')

uni_national_inter <- plm(covid_relevant_1_log ~ 
                            party_new*national_case_pop + 
                            party_new*national_death_pop +
                            party_new*state_covid_policy + 
                            week_new +
                            week_new_2 +
                            week_new_3,            
                          data = plm_df_uni,
                          index = c('user.screen_name', 'week'),
                          model = 'random', 
                          effect = 'twoways')

uni_all_inter <- plm(covid_relevant_1_log ~ 
                       party_new*state_case_pop + 
                       party_new*state_death_pop +
                       party_new*national_case_pop + 
                       party_new*national_death_pop +
                       party_new*state_covid_policy + 
                       week_new +
                       week_new_2 +
                       week_new_3,            
                     data = plm_df_uni,
                     index = c('user.screen_name', 'week'),
                     model = 'random', 
                     effect = 'twoways')

uni_all_inter_ctl <- plm(covid_relevant_1_log ~ 
                           party_new*state_case_pop + 
                           party_new*state_death_pop +
                           party_new*national_case_pop + 
                           party_new*national_death_pop +
                           party_new*state_covid_policy + 
                           np_score +
                           major_cham +
                           week_new +
                           week_new_2 +
                           week_new_3,            
                         data = plm_df_uni,
                         index = c('user.screen_name', 'week'),
                         model = 'random',
                         effect = 'twoways')


# adjust SE for serial correlation -------------------------

uni_state_adj <- coeftest(uni_state, 
                          vcovHC(uni_state, 
                                 method='arellano'))
uni_national_adj <- coeftest(uni_national, 
                             vcovHC(uni_national, 
                                    method='arellano'))
uni_all_adj <- coeftest(uni_all, 
                        vcovHC(uni_all, 
                               method='arellano'))
uni_state_inter_adj <- coeftest(uni_state_inter, 
                                vcovHC(uni_state_inter, 
                                       method='arellano'))
uni_national_inter_adj <- coeftest(uni_national_inter, 
                                   vcovHC(uni_national_inter, 
                                          method='arellano'))
uni_all_inter_adj <- coeftest(uni_all_inter, 
                              vcovHC(uni_all_inter, 
                                     method='arellano'))
uni_all_inter_ctl_adj <- coeftest(uni_all_inter_ctl, 
                                  vcovHC(uni_all_inter_ctl, 
                                         method='arellano'))


###############################################################################
######################## Generate Latex Code : Table S10 ######################
###############################################################################

# get information for the last six rows -------------------------

texreg(
  list(uni_state,
       uni_national,
       uni_all,
       uni_state_inter,
       uni_national_inter,
       uni_all_inter,
       uni_all_inter_ctl),
  digits = 3,
  caption = 'Panel Regression Model (State Fixed Effect and Legislator-week Random Effect): 
  Legislator-governor Partisanship Match, Population Normalized + Three-fold Party Variable + Week Polynomial + Logged DV (1 added)',
  custom.coef.names = c('(Intercept)',
                        'State New Cases (per 10k)',
                        'State New Deaths (per 10k)',
                        'State COVID-19 Policies',
                        #'Other',
                        'Republican',
                        'Week',
                        'Week (quadratic)',
                        'Week (cubic)',
                        'National New Cases (per 10k)',
                        'National New Deaths (per 10k)',
                        #'Other * State New Cases (per 10k)',
                        'Republican * State New Cases (per 10k)',
                        #'Other * State New Deaths (per 10k)',
                        'Republican * State New Deaths (per 10k)',
                        #'Other * State COVID-19 Policies',
                        'Republican * State COVID-19 Policies',
                        #'Other * National New Cases (per 10k)',
                        'Republican * National New Cases (per 10k)',
                        #'Other * National New Deaths (per 10k)',
                        'Republican * National New Deaths (per 10k)',
                        'Legislator Ideology',
                        'Chamber Majority Status'
                        )
  )

# get information for betas and corresponding standard errors -------------------------

texreg(
  list(uni_state_adj,
       uni_national_adj,
       uni_all_adj,
       uni_state_inter_adj,
       uni_national_inter_adj,
       uni_all_inter_adj,
       uni_all_inter_ctl_adj),
  digits = 3,
  caption = 'Panel Regression Model (State Fixed Effect and Legislator-week Random Effect): 
  Legislator-governor Partisanship Match, Population Normalized + Three-fold Party Variable + Week Polynomial + Logged DV (1 added)',
  custom.coef.names = c('(Intercept)',
                        'State New Cases (per 10k)',
                        'State New Deaths (per 10k)',
                        'State COVID-19 Policies',
                        'Other',
                        'Republican',
                        'Week',
                        'Week (quadratic)',
                        'Week (cubic)',
                        'National New Cases (per 10k)',
                        'National New Deaths (per 10k)',
                        'Other * State New Cases (per 10k)',
                        'Republican * State New Cases (per 10k)',
                        'Other * State New Deaths (per 10k)',
                        'Republican * State New Deaths (per 10k)',
                        'Other * State COVID-19 Policies',
                        'Republican * State COVID-19 Policies',
                        'Other * National New Cases (per 10k)',
                        'Republican * National New Cases (per 10k)',
                        'Other * National New Deaths (per 10k)',
                        'Republican * National New Deaths (per 10k)',
                        'Legislator Ideology',
                        'Chamber Majority Status'
                        )
  )